//
//  WCTreeNode.m
//  WabbitCode
//
//  Created by William Towe on 1/10/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCTreeNode.h"
#import "WCTextFile.h"
#import "WCGroup.h"
#import "WCIncludeDirectory.h"
#import "WCBuildDefine.h"
#import "WCBuildTarget.h"
#import "WCBuildStep.h"
#import "WCBuildConfiguration.h"


NSString *kWCTNClassNameKey = @"className";
NSString *kWCTNNodeNameKey = @"name";
NSString *kWCTNChildNodesKey = @"childNodes";

@implementation WCTreeNode
#pragma mark *** Subclass Overrides ***
- (void)dealloc {
	/*
#ifdef WCDEBUG
	static NSUInteger count = 0;
	NSLog(@"WCTreeNode dealloc %u", ++count);
#endif
	 */
	[_name release];
	[super dealloc];
}

- (BOOL)isLeaf {
	return NO;
}
#pragma mark NSCoding Protocol
- (id)initWithCoder:(NSCoder *)coder {
	if (!(self = [super init]))
		return nil;
	
	for (id node in [coder decodeObjectForKey:kWCTNChildNodesKey])
		[[self mutableChildNodes] addObject:node];
	
	_name = [[coder decodeObjectForKey:kWCTNNodeNameKey] retain];
	
	return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
	[coder encodeObject:[self childNodes] forKey:kWCTNChildNodesKey];
	[coder encodeObject:_name forKey:kWCTNNodeNameKey];
}
#pragma mark NSCopying Protocol
- (id)copyWithZone:(NSZone *)zone {
	WCTreeNode *copy = [[[self class] allocWithZone:zone] init];
	
	copy->_childNodes = [_childNodes retain];
	copy->_name = [_name copy];
	
	return copy;
}
#pragma mark NSMutableCopying Protocol
- (id)mutableCopyWithZone:(NSZone *)zone {
	WCTreeNode *copy = [[[self class] allocWithZone:zone] init];
	
	for (id node in [self childNodes])
		[[copy mutableChildNodes] addObject:[[node mutableCopy] autorelease]];
	
	copy->_name = [_name copy];
	
	return copy;
}

#pragma mark *** Public Methods ***
#pragma mark Accessors
@synthesize name=_name;

@dynamic icon;
- (NSImage *)icon {
	return nil;
}

@dynamic filePathInvalidBadge;
- (NSImage *)filePathInvalidBadge {
	return [NSImage imageNamed:@"FilePathInvalidBadge8x8"];
}

@dynamic canRenameNode;
- (BOOL)canRenameNode {
	return YES;
}

@dynamic isValid;
- (BOOL)isValid {
	return YES;
}

@dynamic nodeToolTip;
- (NSString *)nodeToolTip {
	return nil;
}

@dynamic canDeleteNode;
- (BOOL)canDeleteNode {
	return YES;
}

@dynamic canGroupNode;
- (BOOL)canGroupNode {
	return NO;
}

@dynamic canUngroupNode;
- (BOOL)canUngroupNode {
	return NO;
}

@dynamic canGetInfo;
- (BOOL)canGetInfo {
	return YES;
}

@dynamic acceptedDropTypes;
- (NSArray *)acceptedDropTypes {
	return nil;
}

@dynamic canCopyNodeOnDrop;
- (BOOL)canCopyNodeOnDrop {
	return NO;
}

@dynamic commonClass;
- (Class)commonClass {
	return [self class];
}

@dynamic isDragable;
- (BOOL)isDragable {
	return NO;
}

@dynamic isRootLevelGroup;
- (BOOL)isRootLevelGroup {
	return NO;
}

#pragma mark Creation
+ (id)treeNodeWithNodeName:(NSString *)name; {
	return [[[[self class] alloc] initWithNodeName:name] autorelease];
}

+ (id)treeNodeWithDictionary:(NSDictionary *)dict; {
	return [[[[self class] alloc] initWithDictionary:dict] autorelease];
}

- (id)initWithNodeName:(NSString *)name; {
	if (!(self = [super init]))
		return nil;
	
	_name = [name copy];
	
	return self;
}
@end
